home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / gaplib.lha / GAPLib_Beta / examples / TestFunctions.c < prev    next >
C/C++ Source or Header  |  1999-01-10  |  2KB  |  127 lines

  1. /*
  2.  * Five functions which can be used for testing GA:s.
  3.  *
  4.  * Note that the functions negate their value before returning it,
  5.  * this is because GAP-Lib sees higher fitness as better.
  6.  *
  7.  * See also the Images/DeJong?.png
  8.  *
  9.  */
  10.  
  11. #include <GAP.h>
  12. #include <math.h>
  13.  
  14. /* DeJong Function #1 
  15.  * Global minimum at (0,0)
  16.  */
  17.  
  18. struct Polyphant1 {
  19.     unsigned long    x[3];
  20. };
  21.  
  22. double DeJong1(struct Polyphant1 *Polly)
  23. {
  24. int i;
  25. double d=0;
  26. for(i=0;i!=3;i++) {
  27.     d += pow(IRange(Polly->x[i],-5.12,5.12),2.0);    
  28. }
  29. return(-d);
  30. }
  31.  
  32. /* DeJong Function #2 
  33.  * Global minimum at (1,1)
  34.  */
  35.  
  36. struct Polyphant2 {
  37.     unsigned long    x,y;
  38. };
  39.  
  40. double DeJong2(struct Polyphant2 *Polly)
  41. {
  42. double x,y,d;
  43. x = IRange(Polly->x,-2.048,2.048);
  44. y = IRange(Polly->y,-2.048,2.048);
  45. d=100.0*pow((pow(x,2.0)-y),2.0)+pow((1.0-x),2.0);
  46. return(-d);
  47. }
  48.  
  49. /* DeJong Function #3 
  50.  * Global minimum when x<-5.0 and y<-5.0
  51.  */
  52.  
  53. struct Polyphant3 {
  54.     unsigned long    x[5];
  55. };
  56.  
  57. double DeJong3(struct Polyphant3 *Polly)
  58. {
  59. int i;
  60. double d=0;
  61. for(i=0;i!=5;i++) {
  62.     d += floor(IRange(Polly->x[i],-5.12,5.12));    
  63. }
  64. return(-d);
  65. }
  66.  
  67. /* DeJong Function #4
  68.  * Global minimum at (0,0,0,...,0)
  69.  * (Disregarding noise)
  70.  */
  71.  
  72. struct Polyphant4 {
  73.     unsigned long    x[30];
  74. };
  75.  
  76. double DeJong4(struct Polyphant4 *Polly)
  77. {
  78. int i;
  79. double d=0;
  80. for(i=0;i!=30;i++) {
  81.     d += i*pow(IRange(Polly->x[i],-1.28,1.28),4.0)+GaussRand(0.0,1.0);
  82. }
  83. return(-d);
  84. }
  85.  
  86. /* DeJong Function #5
  87.  * Global minimum at (-32,-32)
  88.  */
  89.  
  90. struct Polyphant5 {
  91.     unsigned long    x[2];
  92. };
  93.  
  94. double DeJong5(struct Polyphant5 *Polly)
  95. {
  96. const double K = 500.0;
  97. const double a[2][25] = {
  98.     {-32,-16,  0, 16, 32,
  99.      -32,-16,  0, 16, 32,
  100.      -32,-16,  0, 16, 32,
  101.      -32,-16,  0, 16, 32,
  102.      -32,-16,  0, 16, 32},
  103.  
  104.     {-32,-32,-32,-32,-32,
  105.      -16,-16,-16,-16,-16,
  106.        0,  0,  0,  0,  0,
  107.       16, 16, 16, 16, 16,
  108.       32, 32, 32, 32, 32}
  109. };
  110. double d,o,u;
  111. int i,n;
  112.  
  113. o = 0.0;
  114.  
  115. for(i=0;i!=25;i++) {
  116.     u = (double)i;
  117.     for(n=0;n!=2;n++) {
  118.         u += 1.0/pow(IRange(Polly->x[n],-65.536,65.536)-a[n][i],6.0);
  119.     }
  120.     o += u;
  121. }
  122.  
  123. d = 1.0/(1.0/K+o);
  124.  
  125. return(-d);
  126. }
  127.